home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Visual Basic Source Code
/
Visual Basic Source Code.iso
/
vbsource
/
dirxcpp
/
rmbegin1.cpp
< prev
next >
Wrap
C/C++ Source or Header
|
1998-11-22
|
15KB
|
438 lines
//-----------------------------------------------------------------------------
// File: RMBegin1.cpp
//
// Desc: This minimal file creates a default device, loads a mesh from a
// DirectX file, builds a scene, and rotates the mesh object. It also
// contains standard code which allows the sample to run in the
// Windows environment.
//
// Copyright (C) 1998 Microsoft Corporation. All Rights Reserved.
//
// Modified by Weston Software to demonstrate DirectXCPP
//-----------------------------------------------------------------------------
#define INITGUID // Required for Direct3D applications
#include <windows.h> // Standard Windows header file
#include <direct.h> // DirectDraw definitions
#include "resource.h"
#include "dxcpp.h" // DirectXCPP classes by Weston Software
#include "spider.h" // Animated spider class
// Macro to display a message box containing the given string.
#define DISPLAYMSG(x) MessageBox(NULL, x, "D3DRM Sample", MB_OK);
// Global Variables
CADirect3DRM *lpD3DRM = NULL; // Direct3DRM object
CADirectDrawClipper *lpDDClipper = NULL; // DirectDrawClipper object
// Global Structure
struct _myglobs
{
// Direct3DRM device.
CADirect3DRMDevice * dev;
// Direct3DRM viewport through which to view the scene.
CADirect3DRMViewport * view;
// Master frame in which other frames are placed.
CADirect3DRMFrame * scene;
// Frame describing the users point of view.
CADirect3DRMFrame * camera;
// mesh spider class
CASpider *spider;
// Application is minimized.
BOOL bMinimized;
// All D3DRM objects have been initialized.
BOOL bInitialized;
} myglobs;
// Prototypes
BOOL BuildScene( CADirect3DRM *, CADirect3DRMDevice *,
CADirect3DRMFrame *, CADirect3DRMFrame * );
static BOOL RenderScene();
BOOL CreateObjects( HWND win );
static HWND InitApp( HINSTANCE, int );
int APIENTRY WinMain( HINSTANCE, HINSTANCE, LPSTR, int );
LRESULT CALLBACK WindowProc( HWND, UINT, WPARAM, LPARAM );
//-----------------------------------------------------------------------------
// Name: BuildScene()
// Desc: Create the scene to be rendered.
//-----------------------------------------------------------------------------
BOOL BuildScene( CADirect3DRM * lpD3DRM, CADirect3DRMDevice * dev,
CADirect3DRMFrame * scene, CADirect3DRMFrame * camera )
{
CADirect3DRMFrame *lights = NULL;
CADirect3DRMLight *light1 = NULL;
CADirect3DRMLight *light2 = NULL;
CADirect3DRMFrame rotateframe( lpD3DRM, scene->I() );
rotateframe.SetRotation(scene->I(), D3DVAL(0), D3DVAL(1), D3DVAL(0), D3DVAL(-0.01)); // angle
myglobs.spider = new CASpider( lpD3DRM, rotateframe.I() );
myglobs.spider->SetPosition(rotateframe.I(), D3DVAL(4), D3DVAL(0), D3DVAL(0));
// Set up the camera frame's position. Objects with the same x-value and
// y-value as the camera will appear straight ahead.
// Negative z-values are farther away, making the object look
// smaller as the negative numbers increase.
camera->SetPosition(scene->I(), D3DVAL(0), D3DVAL(8), -D3DVAL(16));
camera->LookAt( rotateframe.I(), scene->I(), D3DRMCONSTRAIN_Z );
// Initialize the lights in the scene, creating a light frame that
// is a child of the scene.
lights = new CADirect3DRMFrame( lpD3DRM, scene->I() );
// Position the light frame within the scene.
lights->SetPosition(scene->I(), D3DVAL(5), D3DVAL(0), -D3DVAL(7));
lights->SetOrientation( scene->I(), 1, -1, 1, 1, 1, 1);
light1 = new CADirect3DRMLight( lpD3DRM, D3DRMLIGHT_DIRECTIONAL,
D3DRGB( D3DVAL(1.0), D3DVAL(0.8), D3DVAL(0.9)));
lights->AddLight( light1->I() );
// Create a dim, ambient light and add it to the scene frame,
// applying it to the whole scene. Ambient light comes from all
// directions, so a bright ambient light would wash out the object.
light2 = new CADirect3DRMLight( lpD3DRM, D3DRMLIGHT_AMBIENT, D3DRGB(
D3DVAL(.1), D3DVAL(0.1), D3DVAL(0.1)));
lights->AddLight( light2->I() );
// setup the shadows on the spider
LPVOID pVoid = NULL;
lpD3DRM->I()->QueryInterface( IID_IDirect3DRM2, (void **) &pVoid );
if (pVoid) // DX5 or greater
{
LPDIRECT3DRMVISUAL pShadow;
lpD3DRM->CreateShadow( myglobs.spider->I(), light1->I(),
D3DVAL(0), D3DVAL(-1), D3DVAL(0),
D3DVAL(0), D3DVAL(1), D3DVAL(0),
&pShadow);
rotateframe.AddVisual( pShadow );
pShadow->Release();
}
else // DX3 has limited shadow support
{
LPDIRECT3DRMVISUAL pShadow;
lpD3DRM->CreateShadow( myglobs.spider->m_pMeshBuilder->I(), light1->I(),
D3DVAL(0), D3DVAL(-1), D3DVAL(0),
D3DVAL(0), D3DVAL(1), D3DVAL(0),
&pShadow);
myglobs.spider->AddVisual( pShadow );
pShadow->Release();
}
// Clean up.
delete lights;
delete light1;
delete light2;
return TRUE;
}
//-----------------------------------------------------------------------------
// Name: RenderScene()
// Desc: Clear the viewport, render the next frame, and update the window.
//-----------------------------------------------------------------------------
static BOOL RenderScene()
{
// Move the scene.
myglobs.scene->Move(D3DVAL(1.0));
myglobs.spider->Tick();
// Clear the viewport.
myglobs.view->Clear();
// Render the scene to the viewport.
myglobs.view->Render(myglobs.scene->I());
// Update the window.
myglobs.dev->Update();
return TRUE;
}
//-----------------------------------------------------------------------------
// Name: AddMediaPath()
// Desc: Looks in the system registry to determine the media path for the
// sample. Then, it adds that path to the string passed in, checks if the
// file exists, and returns a path to the file.
//-----------------------------------------------------------------------------
VOID AddMediaPath( CADirect3DRM * pD3DRM )
{
HKEY key;
LONG result;
TCHAR strPath[512];
DWORD type, size = 512;
// Open the registry
result = RegOpenKeyEx( HKEY_LOCAL_MACHINE, "Software\\Microsoft\\DirectX",
0, KEY_READ, &key );
if( ERROR_SUCCESS != result )
return;
// Search for the desired registry value, and close the registry
result = RegQueryValueEx( key, "DX6SDK Samples Path", NULL, &type,
(BYTE*)strPath, &size );
RegCloseKey( key );
if( ERROR_SUCCESS != result )
return;
strcat( strPath, "\\D3DRM\\Media" );
pD3DRM->AddSearchPath( strPath );
return;
}
//-----------------------------------------------------------------------------
// Name: CreateObjects()
// Desc: Initialize globals, create the device and objects.
//-----------------------------------------------------------------------------
BOOL CreateObjects( HWND win )
{
RECT rc; // Bounding rectangle for main window
int width; // Device's width
int height; // Device's height
// Initialize the entire global variable structure to zero.
memset(&myglobs, 0, sizeof(myglobs));
// Create a DirectDrawClipper object and associate the window with it.
lpDDClipper = new CADirectDrawClipper();
lpDDClipper->SetHWnd(0, win);
// Create the Direct3DRM object.
lpD3DRM = new CADirect3DRM();
// Create a default Direct3DRM device.
GetClientRect(win, &rc);
myglobs.dev = new CADirect3DRMDevice( lpD3DRM, lpDDClipper->I(),
rc.right, rc.bottom);
// Create the master scene frame and the camera frame.
myglobs.scene = new CADirect3DRMFrame( lpD3DRM, NULL );
myglobs.scene->SetSceneBackgroundRGB( D3DVAL(0), D3DVAL(0), D3DVAL(.4) );
myglobs.camera = new